package com.action;

import com.entity.Admin;
import com.service.AdminService;
import com.util.Const;
import com.util.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

/**
 * 
 */

@Controller
@Scope("prototype")
public class AdminAction extends BaseAction 
{

    @Autowired
    private AdminService adminService;


    private Admin admin;
    private PageBean pageBean=new PageBean();
    private Integer id;


    //作用域
    private List<Admin> admins = new ArrayList<Admin>();


    public String login()
    {
        String userName=admin.getUsername();
        String pasword=admin.getPassword();
        Admin admin=adminService.login(userName,pasword);
        if(admin!=null)
        {
            session.put("admin",admin);
            return "list";
        }
        else 
        {
            try 
            {
                response.setContentType("text/html;charset=UTF-8");
                PrintWriter out= response.getWriter();
                out.print("<script> alert('用户名或密码错误！');location.href='"+ Const.ROOT+"managers/login.jsp'</script>");
            } 
            catch (IOException e) 
            {
                e.printStackTrace();
            }
            return null;
        }
    }

    public String list() 
    {
        String hql="from Admin where 1=1 ";
        String hqlCount="select count(*) from Admin where 1=1 ";
        pageBean.setTotalCount(adminService.getCount(hqlCount));
        admins = adminService.getAll(hql,pageBean.pageNo,pageBean.pageSize);
        return "index";
    }

    public String add()
    {
        adminService.addAdmin(admin);
        return "list";
    }
    
    public String toupdate()
    {
        admin =adminService.getAdminById(id);
        return SUCCESS;
    }

    public String doupdate()
    {
        adminService.updateAdmin(admin);
        return "list";
    }
    
    public String delete()
    {
    	adminService.delById(id);
        return "list";
    }

    public AdminService getAdminService() 
    {
        return adminService;
    }

    public void setAdminService(AdminService adminService) 
    {
        this.adminService = adminService;
    }

    public Admin getAdmin() 
    {
        return admin;
    }

    public void setAdmin(Admin admin) 
    {
        this.admin = admin;
    }

    public PageBean getPageBean() 
    {
        return pageBean;
    }

    public void setPageBean(PageBean pageBean) 
    {
        this.pageBean = pageBean;
    }

    public List<Admin> getAdmins() 
    {
        return admins;
    }

    public void setAdmins(List<Admin> admins) 
    {
        this.admins = admins;
    }
    
    public Integer getId() 
    {
        return id;
    }

    public void setId(Integer id) 
    {
        this.id = id;
    }
}
